//
//  CouponViewController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/17.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "CouponViewController.h"
#import "CouponCell.h"
#import "CouponModel.h"
@interface CouponViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, assign) BOOL leftOrRight;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dataArray2;
@end

@implementation CouponViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.title = @"我的礼包";
    
    [self requestData];
}

- (void)requestData {
    self.dataArray = [NSMutableArray array];
    self.dataArray2 = [NSMutableArray array];
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1029&sign=%@",API_LOGIN_URL_STRING,USER_SIGN];
    NSLog(@"%@",urlString);
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        NSInteger code = [dict[@"code"] integerValue];
        if (code == -1) {
            [self.view makeToast:dict[@"msg"]];
        } else {
            for (NSDictionary *data in dict[@"list"]) {
                CouponModel *model = [CouponModel mj_objectWithKeyValues:data];
                NSLog(@"%ld",model.money);
                [self.dataArray addObject:model];
            }
        
            CouponModel *model2 = [[CouponModel alloc] init];
            model2.money = [dict[@"xstyj"] integerValue];
            model2.yxq_date = dict[@"xstyDate"];
            
            [self.dataArray2 addObject:model2];
        }
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}

- (IBAction)changeData:(UIButton *)sender {
    if (sender.tag == 101) {
        _line.frame = CGRectMake(0, _line.y, _line.width, _line.height);
        [sender setTitleColor:THE_MASS_TONE_ATTUNE forState:UIControlStateNormal];
          [_rightBtn setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
        _leftOrRight = NO;
        [self.tableView reloadData];
    } else if (sender.tag == 102) {
        _line.frame = CGRectMake(_line.width, _line.y, _line.width, _line.height);
        [sender setTitleColor:THE_MASS_TONE_ATTUNE forState:UIControlStateNormal];
        [_leftBtn setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
        _leftOrRight = YES;
        [self.tableView reloadData];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_leftOrRight) {
        return self.dataArray.count;
    } else {
        return self.dataArray2.count;
    }
    
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"coupon_cell" forIndexPath:indexPath];
    if (_leftOrRight) {
        CouponModel *model = self.dataArray[indexPath.row];
        [cell setModel:model];
    } else {
        CouponModel *model = self.dataArray2[indexPath.row];
        [cell setModel:model];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_leftOrRight) {
        return 107;
    } else {
        return 100;
    }
}

@end
